﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using App.Metrics;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace Gateway.API
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var authenticationKey = "finbook";
            services.AddOcelot();
            services.AddAuthentication()
                .AddIdentityServerAuthentication(authenticationKey, opt =>
                {
                    opt.ApiName = "user_api";
                    opt.ApiSecret = "p@ssw0rd";
                    opt.Authority = "http://localhost:8002";
                    opt.SaveToken = true;
                    opt.SupportedTokens = IdentityServer4.AccessTokenValidation.SupportedTokens.Both;
                    opt.RequireHttpsMetadata = false;
                });


            var builder = AppMetrics.CreateDefaultBuilder()
                .Configuration
                .Configure(opt =>
                {
                    opt.DefaultContextLabel = "finbook";
                    opt.GlobalTags.Add("ApplicationName", "finbook");
                    opt.ReportingEnabled = true;
                    opt.Enabled = true;
                    opt.AddEnvTag("dev");
                    opt.AddAppTag("finbook");
                })
                .Report.ToInfluxDb(opt =>
                {
                    opt.InfluxDb.BaseUri = new Uri("http://140.143.88.100:8086");
                    opt.InfluxDb.Database = "verticalsite";
                    opt.InfluxDb.UserName = "Zj";
                    opt.InfluxDb.Password = "p@ssw0rd";
                    opt.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30);
                    opt.HttpPolicy.Timeout = TimeSpan.FromSeconds(10);
                    opt.HttpPolicy.FailuresBeforeBackoff = 5;
                    opt.FlushInterval = TimeSpan.FromSeconds(5);
                })
                .Build();
            services.AddMetrics(builder);
            services.AddMetricsEndpoints();
            services.AddMetricsReportScheduler();
            services.AddMetricsTrackingMiddleware();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseAuthentication();
            app.UseOcelot().Wait();
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
            //app.UseMetricsAllMiddleware();
            //app.UseMetricsAllEndpoints();

            app.UseMetricsAllMiddleware();
            // Or to cherry-pick the tracking of interest
            app.UseMetricsActiveRequestMiddleware();
            app.UseMetricsErrorTrackingMiddleware();
            app.UseMetricsPostAndPutSizeTrackingMiddleware();
            app.UseMetricsRequestTrackingMiddleware();
            app.UseMetricsOAuth2TrackingMiddleware();
            app.UseMetricsApdexTrackingMiddleware();

            app.UseMetricsAllEndpoints();
            // Or to cherry-pick endpoint of interest
            app.UseMetricsEndpoint();
            app.UseMetricsTextEndpoint();
            app.UseEnvInfoEndpoint();
        }
    }
}
